// Dashboard actions page
// Methods : AddPanelName, SavePanel, ApplyDashboardBtn

export default class DashboardactionPage {
  constructor(page) {
    this.page = page;

    this.panelNameInput = page.locator('[data-test="dashboard-panel-name"]');
    this.panelSaveBtn = page.locator('[data-test="dashboard-panel-save"]');
    this.applyDashboard = page.locator('[data-test="dashboard-apply"]');
  }

  // Generate a unique panel name
  generateUniquePanelName(prefix = "panel") {
    const randomStr = Math.random().toString(36).substr(2, 5);
    return `${prefix}_${Date.now()}_${randomStr}`;
  }

  // Add panel name
  async addPanelName(panelName) {
    await this.panelNameInput.click();
    await this.panelNameInput.fill(panelName);
  }

  // Save panel button
  async savePanel() {
    await this.panelSaveBtn.waitFor({ state: "visible" });
    await this.panelSaveBtn.click();
  }

  //Apply dashboard button
  async applyDashboardBtn() {
    await this.applyDashboard.click();
  }

  // Wait for chart to render

  async waitForChartToRender() {
    // Wait for button or parent to have light button class (render complete)
    // Supports both repo structures: button.o2-primary-button-light OR parent.o2-secondary-button-light
    await this.page.waitForFunction(() => {
      const btn = document.querySelector('[data-test="dashboard-apply"]');
      return (
        btn &&
        (btn.classList.contains("o2-primary-button-light") ||
          btn.classList.contains("o2-secondary-button-light") ||
          (btn.parentElement &&
            (btn.parentElement.classList.contains("o2-primary-button-light") ||
              btn.parentElement.classList.contains("o2-secondary-button-light"))))
      );
    });
  }

  //Dashboard panel actions(Edit, Layout, Duplicate, Inspector, Move, Delete)

  async selectPanelAction(panelName, action) {
    const actionDataTestIds = {
      Edit: "dashboard-edit-panel",
      Layout: "dashboard-edit-layout",
      Duplicate: "dashboard-duplicate-panel",
      Inspector: "dashboard-query-inspector-panel",
      Move: "dashboard-move-to-another-panel",
      Delete: "dashboard-delete-panel",
    };

    const actionTestId = actionDataTestIds[action];
    if (!actionTestId) throw new Error(`Unknown action: ${action}`);

    await this.page
      .locator(`[data-test="dashboard-edit-panel-${panelName}-dropdown"]`)
      .click();
    await this.page.locator(`[data-test="${actionTestId}"]`).click();
  }
}
